<?php
namespace Biz\model;

use Medoo\Medoo;
use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\pockets\publicTrait\CheckConstants;

class ShopFavorite extends BaseDbModel
{
	use CheckConstants;
	const _CONFIG_FILE_ = 'db_shop';

	const _CONFIG_SELECT_ = 'db_shop';

	const _DATABASE_ = 'shop';

	const _TABLE_NAME = 'shop_favorite';

	const STATUS_NORMAL  = 1;
	const STATUS_DEL  = 9;

	const TYPE_GOODS = 1; //商品
	const TYPE_ROOM = 2;  //空间
	const TYPE_WIKI = 3;  //wiki

	const APP_SOURCE_APP = 0;
	const APP_SOURCE_MINIPROGRAME = 5;

	const _CONSTANTS_TYPE_PREFIX = 'TYPE_';

	const DEFAULT_PAGESIZE = 20;

	public static function is_favorited($uid, array $objIds, $objType, $appSource = 0)
	{
		if (!self::checkConstants(self::_CONSTANTS_TYPE_PREFIX, $objType)) {
			throw new \Exception('类型错误', __LINE__);
		}
		$favObjIds = self::select('obj_id', [
			'uid' => $uid,
			'obj_id' => $objIds,
			'obj_type' => $objType,
			'app_source' => $appSource,
			'status' => self::STATUS_NORMAL
		]);
		$is_fav = [];
		foreach ($objIds as $objId) {
			if (in_array($objId, $favObjIds)) {
				$is_fav[$objId] = 1;
			} else {
				$is_fav[$objId] = 0;
			}
		}
		return $is_fav;
	}


	public static function add_favorite($uid, $objId, $objType, $appSource)
	{
		if (!self::checkConstants(self::_CONSTANTS_TYPE_PREFIX, $objType)) {
			throw new \Exception('类型错误', __LINE__);
		}
		if (self::get('*', ['uid' => $uid, 'obj_id' => $objId, 'obj_type' => $objType,'app_source' => $appSource])) {

			return self::update([
				'status' => self::STATUS_NORMAL,
				'add_time' => date("Y-m-d H:i:s")
			], [
				'uid' => $uid,
				'obj_id' => $objId,
				'obj_type' => $objType,
				'app_source' => $appSource
			]) -> rowCount();

		} else {
			self::insert([
				'uid' => $uid,
				'obj_id' => $objId,
				'obj_type' => $objType,
				'app_source' => $appSource,
				'add_time' => date("Y-m-d H:i:s"),
				'status' => self::STATUS_NORMAL,
			]);

			return self::id();
		}
	}

	public static function UnFavorite($uid, $objId, $objType, $appSource = 0)
	{
		if (!self::checkConstants(self::_CONSTANTS_TYPE_PREFIX, $objType)) {
			throw new \Exception('类型错误', __LINE__);
		}

		return self::update([
			'status' => self::STATUS_DEL
		], [
			'uid' => $uid,
			'obj_id' => $objId,
			'obj_type' => $objType,
			'app_source' => $appSource
		])->rowCount();

	}

	public static function getAllFav($uid, $objType, $appSource)
    {
        return self::select(['id', 'obj_type', 'obj_id'], [
            'uid' => $uid,
            'obj_type' => $objType,
            'app_source' => $appSource,
            'status' => self::STATUS_NORMAL,
            'ORDER' => ['add_time' => 'DESC'],
        ]);
    }
    public static function getFavInfosByIds($ids, $page, $pagesize = self::DEFAULT_PAGESIZE)
    {

        $favInfos = self::select('*', [
            'id' => $ids,
            'status' => self::STATUS_NORMAL,
            'ORDER' => ['add_time' => 'DESC'],
            'LIMIT' => [($page - 1) * $pagesize, $pagesize]
        ]);

        return [
            'list' => $favInfos,
            'is_over' => count($favInfos) < $pagesize ? 1 : 0
        ];
    }
	public static function getFavInfos($uid, $objType, $appSource, $page, $pagesize = self::DEFAULT_PAGESIZE)
	{

		$favInfos = self::select('*', [
			'uid' => $uid,
			'obj_type' => $objType,
			'app_source' => $appSource,
			'status' => self::STATUS_NORMAL,
			'ORDER' => ['add_time' => 'DESC'],
			'LIMIT' => [($page - 1) * $pagesize, $pagesize]
		]);

		return [
			'list' => $favInfos,
			'is_over' => count($favInfos) < $pagesize ? 1 : 0
		];
	}

	public static function getFavCountByGoodIds($goodsIds, $field = '*')
    {
        $goodsList = self::select($field, [
            'AND' => [
                'obj_id' => $goodsIds,
                'obj_type' => self::TYPE_GOODS,
                'status' => self::STATUS_NORMAL,
            ]
        ]);

        $total = [];
        if($goodsList) {
            foreach ($goodsList as $goodsId) {
                $total[$goodsId][] = $goodsId;
            }
        }

        return $total;
    }

    public static function getFavCounterByWikiIds($wiki_ids)
    {
        $response = self::select(['obj_id', 'count' => Medoo::raw("count(*)")], [
            'AND' => [
                'obj_type' => self::TYPE_WIKI,
                'app_source' => self::APP_SOURCE_APP,
                'obj_id' => $wiki_ids,
                'status' => self::STATUS_NORMAL
            ],
            'GROUP' => ['obj_id']
        ]);
        return $response;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);
        return $count;
    }

}




